package com.itheima.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.itheima.domain.User;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.Date;

@Repository
public interface UserDao extends BaseMapper<User> {
    @Results({
            @Result(property = "userId", column = "user_id"),
            @Result(property = "realName", column = "real_name"),
            @Result(property = "studentId", column = "student_id"),
            @Result(property = "registerTime", column = "register_time"),
            @Result(property = "lastLoginTime", column = "last_login_time")
    })
    @Select("SELECT * FROM user WHERE username = #{username}")
    User findByUsername(String username);

    @Results({
            @Result(property = "userId", column = "user_id"),
            @Result(property = "realName", column = "real_name"),
            @Result(property = "studentId", column = "student_id"),
            @Result(property = "registerTime", column = "register_time"),
            @Result(property = "lastLoginTime", column = "last_login_time")
    })

    @Select("SELECT * FROM user WHERE user_id = #{userId}")
    User findByUserId(int userId);

    @Results({
            @Result(property = "realName", column = "real_name"),
            @Result(property = "userId", column = "user_id")
    })
    @Select("SELECT real_name From user WHERE user_id = #{userId}")
    String getUserRealName(@Param("userId") int userId);

    @Select("select phone from user where user_id = #{userId}")
    String getProductSellerPhone(@Param("userId") int userId);

    @Update("UPDATE user SET last_login_time = #{lastLoginTime} WHERE user_id = #{userId}")
    void updateLoginTime(@Param("userId") Integer userId, @Param("lastLoginTime") Date lastLoginTime);

    @Insert("INSERT INTO user (username, password, real_name, student_id, email, phone, avatar, register_time) " +
            "VALUES (#{username}, #{password}, #{realName}, #{studentId}, #{email}, #{phone}, #{avatar}, #{registerTime})")
    int insertUser(User user);

    // 检查用户名是否存在
    @Select("SELECT COUNT(*) FROM user WHERE username = #{username}")
    int countByUsername(@Param("username") String username);

    // 检查邮箱是否存在
    @Select("SELECT COUNT(*) FROM user WHERE email = #{email}")
    int countByEmail(@Param("email") String email);

    // 检查学号是否存在
    @Select("SELECT COUNT(*) FROM user WHERE student_id = #{studentId}")
    int countByStudentId(@Param("studentId") String studentId);

    @Select("SELECT COUNT(*) FROM user WHERE phone = #{phone}")
    int countByPhone(@Param("phone") String phone);

    @Select("select avatar from user where user_id = #{userId}")
    String getUserAvatar(@Param("userId") int userId);

    @Update("UPDATE user SET username=#{username},password=#{password},real_name=#{realName},student_id=#{studentId},email=#{email},phone=#{phone},address = #{address} where user_id=#{userId}")
    int updateUser(User user);

    @Update("update user set avatar = #{avatar} where user_id = #{userId}")
    int updateAvatar(@Param("avatar") String avatar, @Param("userId") int userId);

    @Update("UPDATE user SET address = #{address} WHERE user_id = #{userId}")
    int updateAddress(@Param("address") String address, @Param("userId") int userId);

    @Update("UPDATE user SET phone = #{phone} WHERE user_id = #{userId}")
    int updatePhone(@Param("phone") String phone, @Param("userId") int userId);

    // 检查其他用户名是否存在
    @Select("SELECT COUNT(*) FROM user WHERE username = #{username} AND user_id != #{userId}")
    int countElseByUsername(@Param("username") String username, @Param(("userId")) int userId);

    // 检查其他邮箱是否存在
    @Select("SELECT COUNT(*) FROM user WHERE email = #{email} AND user_id != #{userId}")
    int countElseByEmail(@Param("email") String email, @Param(("userId")) int userId);

    // 检查其他学号是否存在
    @Select("SELECT COUNT(*) FROM user WHERE student_id = #{studentId} AND user_id != #{userId}")
    int countElseByStudentId(@Param("studentId") String studentId, @Param(("userId")) int userId);

    @Select("SELECT COUNT(*) FROM user WHERE phone = #{phone} AND user_id != #{userId}")
    int countElseByPhone(@Param("phone") String phone, @Param(("userId")) int userId);

}
